package actions;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;
import service.ExpressShipPathService;
import domain.ExpressShipPath;
/**
 *@author system auto
 */
public class ExpressShipPathCreateAction extends Action {
	/**
	 * 快递线路service.
	 */
	private ExpressShipPathService expressShipPathService = null;
	/**
	 * 创建快递线路.
	 * @param mapping action mapping
	 * @param form action form
	 * @param request http request
	 * @param response http response
	 * @return action forword
	 * @throws Exception any exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
	        HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
	    // 验证是否是有效的Token,防止重复提交
	    if (!isTokenValid(request, true)) {
	    	// 重复提交,转到重复提交失败界面
	        return mapping.findForward("error.duplicateSubmit");
	     }
	    try{
	        DynaValidatorForm expressShipPathForm = (DynaValidatorForm) form;
	        ExpressShipPath expressShipPath = new ExpressShipPath();
	        this.convertBean(expressShipPath,expressShipPathForm);
	        expressShipPathService.addExpressShipPath(expressShipPath);
	      }catch(Exception e){
			ActionErrors errors=new ActionErrors();
	        errors.add("errors.data.operate.error", new ActionMessage("errors.data.operate.error",utils.DaoUtil.exceptionToStr(e)));
	        this.addErrors(request, errors);
	        // 设置token 防止重复提交
	        this.saveToken(request);
	        return mapping.getInputForward();
	      }
	      return mapping.findForward("success");
	}
	/**
	 *convertBean
	 */
	 private void convertBean(ExpressShipPath expressShipPath, DynaValidatorForm expressShipPathForm)
	 		throws Exception{
	    	String startPos=expressShipPathForm.getString("startPos");
	        if(startPos!=null&&!startPos.equals("")){
	        	expressShipPath.setStartPos(startPos);
	        }
	    	String endPos=expressShipPathForm.getString("endPos");
	        if(endPos!=null&&!endPos.equals("")){
	        	expressShipPath.setEndPos(endPos);
	        }
	    	String serviceType=expressShipPathForm.getString("serviceType");
	        if(serviceType!=null&&!serviceType.equals("")){
	        	expressShipPath.setServiceType(serviceType);
	        }
	    	String transPortType=expressShipPathForm.getString("transPortType");
	        if(transPortType!=null&&!transPortType.equals("")){
	        	expressShipPath.setTransPortType(transPortType);
	        }
	    	String roadDistance=expressShipPathForm.getString("roadDistance");
	        if(roadDistance!=null&&!roadDistance.equals("")){
	        	expressShipPath.setRoadDistance(Double.valueOf(roadDistance));
	        }
	    	String estimateHours=expressShipPathForm.getString("estimateHours");
	        if(estimateHours!=null&&!estimateHours.equals("")){
	        	expressShipPath.setEstimateHours(Double.valueOf(estimateHours));
	        }
	    	String estimateCost=expressShipPathForm.getString("estimateCost");
	        if(estimateCost!=null&&!estimateCost.equals("")){
	        	expressShipPath.setEstimateCost(Double.valueOf(estimateCost));
	        }
	    	String quotedPrice=expressShipPathForm.getString("quotedPrice");
	        if(quotedPrice!=null&&!quotedPrice.equals("")){
	        	expressShipPath.setQuotedPrice(Double.valueOf(quotedPrice));
	        }
	    	String urgentPrice=expressShipPathForm.getString("urgentPrice");
	        if(urgentPrice!=null&&!urgentPrice.equals("")){
	        	expressShipPath.setUrgentPrice(Double.valueOf(urgentPrice));
	        }
	    	String noteDesc=expressShipPathForm.getString("noteDesc");
	        if(noteDesc!=null&&!noteDesc.equals("")){
	        	expressShipPath.setNoteDesc(noteDesc);
	        }
	 }
	/**
     * @param pExpressShipPathService the expressShipPathService to set
     */
    public void setExpressShipPathService(ExpressShipPathService pExpressShipPathService) {
        expressShipPathService = pExpressShipPathService;
    }
}
